 /*
 * Copyright (C) 2012 TheLionsRoar <thelionwillalwaysroar@gmail.com>
 * Copyright (C) 2012 Jedrix <jedrix2454@gmail.com>
 * 
 * This file is part of the LEO Game Library.
 * 
 * LEO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * LEO is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have receieved a copy of the GNU General Public License
 * along with LEO. If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace LEO.Framework {
    public class SoundEffect2D : GameComponent {
        public SoundEffectInstance Sound;

        public int DistanceScale { get; set; }
        public SoundEffect soundEffect { get; set; }
        public Vector3 Position;
        public bool Looped { get; set; }
        public bool Is2D { get; set; }

        public SoundEffect2D(Game game, SoundEffect sound, Vector3 position, int distanceScale)
            : base(game) {
            DistanceScale = distanceScale;
            soundEffect = sound;
            Position = position;
            game.Components.Add(this);
        }

        public override void Update(GameTime gameTime) {
            if(!Is2D) {
                Sound.Apply3D(Camera.CurrentCamera.AudioListener,
                              new AudioEmitter {
                                  Forward = Vector3.Forward,
                                  Position = Position,
                                  Up = Vector3.Up,
                                  Velocity = new Vector3()
                              });
            }
            base.Update(gameTime);
        }

        public void PlaySound() {
            Sound = soundEffect.CreateInstance();

            Sound.IsLooped = Looped;
            if(!Is2D) {
                Sound.Apply3D(Camera.CurrentCamera.AudioListener, new AudioEmitter { Forward = Vector3.Forward, Position = Position, Up = Vector3.Up, Velocity = new Vector3() });
            }
            SoundEffect.DistanceScale = DistanceScale;
            Sound.Play();
        }

        public void StopSound(bool immediately) {
            Sound.Stop(immediately);
        }

        public void ResumeSound() {
            Sound.Resume();
        }

        public void PauseSound() {
            Sound.Pause();
        }
    }
}
